﻿using KinonekoSoftware.CodariaDB.Documents.DataParts.Modules.RVE;
using KinonekoSoftware.CodariaDB.Documents.Theories.Rarities;

namespace KinonekoSoftware.Studio.Models.Modules.RVE
{
    public sealed class RVERarityRibbonPresenter : RVE_Presenter<RVE_RarityRibbon>
    {
        private Rarity _rarity;

        public RVERarityRibbonPresenter()
        {
            var collection = Platform.GetEngine<TheoryEngine_Rarity>()
                                     .Collection;
            _rarity = collection.Count > 0 ? collection[0] : null;

            AssociatedObject = new RVE_RarityRibbon
            {
                Id       = ID.Get(),
                RarityID = _rarity?.Id,
            };
        }

        public RVERarityRibbonPresenter(RVE_RarityRibbon rve_rarity, Rarity rarity)
        {
            _rarity          = rarity ?? throw new ArgumentNullException(nameof(rarity));
            AssociatedObject = rve_rarity;
            Title            = rve_rarity.Title;
            Description      = rve_rarity.Description;
            RaiseUpdated(nameof(Color));
            RaiseUpdated(nameof(Value));
            RaiseUpdated(nameof(Level));
            RaiseUpdated(nameof(Icon));
            RaiseUpdated(nameof(Foreground));
            RaiseUpdated(nameof(Stroke));
        }


        public override string GetTitle() => string.IsNullOrEmpty(Title) ? CodariaSR.Module_RVE_Rarity : Title;
        public override bool IsMultipleVariants() => false;
        public override IEnumerable<Variant> GetVariants(string documentID) => null;
        public override Variant GetVariant(string documentID)
        {
            return new StringVariant
            {
                Id       = documentID,
                Metadata = KnownMetadatas.Rarity,
                Value    = Rarity?.Name,
                Type     = VariantType.String,
            };
        }

        public Rarity Rarity
        {
            get => _rarity;
            set
            {
                if (value is null)
                {
                    return;
                }

                _rarity                   = value;
                AssociatedObject.RarityID = value.Id;
                RaiseUpdated();
                RaiseUpdated(nameof(Color));
                RaiseUpdated(nameof(Value));
                RaiseUpdated(nameof(Level));
                RaiseUpdated(nameof(Icon));
                RaiseUpdated(nameof(Foreground));
                RaiseUpdated(nameof(Stroke));
            }
        }

        public string Color      => _rarity?.Background ?? "#007ACC";
        public string Value      => _rarity?.Name       ?? "缺失稀有度";
        public int    Level      => _rarity?.Level      ?? 1;
        public string Icon       => _rarity?.Icon;
        public string Stroke     => _rarity?.Stroke;
        public string Foreground => _rarity?.Foreground;
    }
}